home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18260 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  82 lines

  1. Path: kbad.eglin.af.mil!rpi!not-for-mail
  2. From: floydb1@lib104.its.rpi.edu (Barry B Floyd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: String operator+ and memory leakage.
  5. Date: 19 Apr 1996 12:27:42 -0400
  6. Organization: Rensselaer Polytechnic Institute, Troy, NY.
  7. Message-ID: <4l8etu$vu@lib104.its.rpi.edu>
  8. References: <4l5fok$feo@utopia.hacktic.nl>
  9. NNTP-Posting-Host: lib104.its.rpi.edu
  10. X-newsreader: xrn 7.04-beta-11
  11.  
  12.  
  13. ------------------------------------------------------------------------
  14. in file:
  15.     ~gnu/libg++/2.7.0a/distrib/src/libg++/src/String.h
  16.  
  17.  
  18. inline     String operator+ (const String& x, const char* y) 
  19.     { String r ; cat(x, y, r) ; return r ; }
  20.  
  21. inline     String operator+ (const char* x, const String& y) 
  22.     { String r ; cat(x, y, r) ; return r ; }
  23.  
  24.  
  25.  
  26. inline     String& String::operator = (const String& y)
  27.     { rep = Scopy(rep, y.rep) ;
  28.         return *this ; }
  29.  
  30. inline     String& String::operator= (const char* t)
  31.     { rep = Salloc(rep, t, -1, -1) ;
  32.         return *this ; }
  33.  
  34.  
  35.  
  36. inline     static void scopy(const char* from, char* to)
  37.     { if (from != 0) while((*to++ = *from++) != 0) ; }
  38.  
  39.  
  40. struct     StrRep                     // internal String representations
  41.     {
  42.       unsigned short    len ;  // string length 
  43.       unsigned short    sz ;   // allocated space
  44.       char              s[1] ; // the string starts here  (at 
  45.                                    //   least 1 char for trailing 
  46.                                    //   null) allocated & expanded 
  47.                    //   via non-public fcts
  48.     } ;
  49.  
  50. ------------------------------------------------------------------------
  51. in file:
  52.     ~gnu/libg++/2.7.0a/distrib/src/libg++/src/String.cc
  53.  
  54. StrRep *Salloc(StrRep* old, const char* src, int srclen, int newlen)
  55.     { // allocate, copying src if nonull
  56.  
  57.       if (old == &_nilStrRep) old = 0 ;
  58.       if (srclen < 0) srclen = slen(src) ;
  59.       if (newlen < srclen) newlen = srclen ;
  60.  
  61.       StrRep *rep ;
  62.  
  63.       if (old == 0 || newlen > old->sz)
  64.         rep = Snew(newlen) ;
  65.       else
  66.         rep = old ;
  67.  
  68.       rep->len = newlen ;
  69.       ncopy0(src, rep->s, srclen) ;
  70.  
  71.       if (old != rep && old != 0) 
  72.         delete old ;
  73.  
  74.       return rep ;
  75.     }
  76.  
  77. -- 
  78. +--------------------------------------------------------------------+ 
  79. | Barry B. Floyd                   \\\               floydb1@rpi.edu |
  80. | RPI Alum. '84 '87 '88              \\\                             |
  81. +--------------------------------------------------------------------+
  82.